package com.leanote.berial.tingphone.ui.fragment;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.leanote.berial.tingphone.Constants;
import com.leanote.berial.tingphone.R;
import com.leanote.berial.tingphone.adapter.CommonFragmentPagerAdapter;
import com.leanote.berial.tingphone.base.BaseFragment;
import com.leanote.berial.tingphone.bean.DiscoverTab;
import com.leanote.berial.tingphone.parsers.DataParser;
import com.leanote.berial.tingphone.tasks.TaskCallback;
import com.leanote.berial.tingphone.tasks.TaskResult;
import com.leanote.berial.tingphone.tasks.impl.DiscoverTabTask;
import com.leanote.berial.tingphone.ui.fragment.discover.*;
import com.leanote.berial.tingphone.util.LogUtil;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by anany on 15/7/28.
 */
public class DiscoverFragment extends BaseFragment implements TabLayout.OnTabSelectedListener, TaskCallback {
    //放子栏目的
    private ViewPager pager;
    //子栏目的tab指示器
    private TabLayout tabBar;

    /**
     * tab信息
     */
    private List<DiscoverTab> tabTitles;

    /**
     *
     */
    private List<Fragment> subFragment;

    public DiscoverFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        tabTitles = new ArrayList<DiscoverTab>();
        subFragment = new ArrayList<Fragment>();
    }

    @Override
    public View initView(LayoutInflater inflater,ViewGroup container) {
        View ret = inflater.inflate(R.layout.fragment_discover, container, false);
        //TODO 搜索按钮 的实现
////        ImageButton search = (ImageButton) ret.findViewById(R.id.discovr_search);
//        if (search != null) {
//            search.setOnClickListener(this);
//        }
        tabBar = (TabLayout) ret.findViewById(R.id.discover_tab_bar);
        //TabLayout创建一个tab实例
        //TODO 因为tab的设置是从网上来的 因此需要动态添加,所以ViewPager Adapter也需要动态设置

        pager = (ViewPager) ret.findViewById(R.id.discover_pager);
        //设置adapter 动态处理

        //viewPager在滑动切换的时候  添加监听
        //监听由tabLayoutOnChangeListener 完成
        //从而实现ViewPager滚动 上面的tabLayout跟随滚动
        pager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabBar));
        tabBar.setOnTabSelectedListener(this);

        //加载网络获取的Tabs
        DiscoverTabTask tabTask = new DiscoverTabTask(this);
        tabTask.execute();
        return ret;
    }

    @Override
    public void initData() {
        if(!isVisible){
            return;
        }
        LogUtil.i("加载发现数据");
    }
//    @Override
//    public void onClick(View v) {
//
//    }

    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        int position = tab.getPosition();
        pager.setCurrentItem(position);

    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {
        //TODO 进行刷新

    }

    @Override
    public void onTaskFinished(TaskResult result) {
        if (result != null) {
            int taskId = result.taskId;
            Object data = result.data;
            if (taskId == Constants.TASK_DISCOVER_TAB_TASK) {
                if (data != null) {
                    if (data instanceof JSONObject) {
                        JSONObject jsonObject = (JSONObject) data;
                        int code = 0;
                        tabTitles = DataParser.parseDiscoverTabs(jsonObject);
                        updateTabs();
                    }
                }else {

                }
            }
        }
    }

    private void updateTabs(){
        if (tabTitles != null) {

            for (DiscoverTab tabTitle : tabTitles) {
                TabLayout.Tab tab = tabBar.newTab();
                tab.setText(tabTitle.getTitle());
                tabBar.addTab(tab);
                //根据内容类型来设置指定的Fragment
                String type = tabTitle.getContentType();
                if ("recommend".equals(type)){
                    subFragment.add(new DiscoverRecommendFragment());
                }else if("category".equals(type)){
                    subFragment.add(new DiscoverCategoryFragment());
                }else if("live".equals(type)){
                    subFragment.add(new DiscoverLiveFragment());
                }else if ("ranking".equals(type)){
                    subFragment.add(new DiscoverRankingFragment());
                }else if ("anchor".equals(type)){
                    subFragment.add(new DiscoverAnchorFragment());
                }
            }
            CommonFragmentPagerAdapter adapter = new CommonFragmentPagerAdapter(getChildFragmentManager(),subFragment);
            pager.setAdapter(adapter);
        }
    }
}
